clc
clear

T=1;
w=1/T;
t = linspace(0,pi,1000); 
x = sawtooth(2*pi*t/T, 0.5);


% 傅里叶级数近似
k = 1:2:41; % 
fourier_x = 0; 
for i = 1:1:20
    fourier_x= fourier_x-8/(k(i)^2 * pi^2) * cos(2*pi*k(i)*t/T);
end

figure(1);
plot(t, fourier_x, 'b'); 
grid on;



%w<<1/RC
R=100;
C=0.001;
H_hp_p = (1i*k*2*pi*R*C/T)./(1+1i*k*2*pi*R*C/T);
r_p = abs(H_hp_p);
theta_p = angle(H_hp_p);

fourier_x_hp=0;
for i = 1:1:20
    fourier_x_hp = fourier_x_hp - 8*r_p(i)/(k(i)^2 * pi^2) * cos(2*pi*k(i)*t/T + theta_p(i));
end
figure(2)
plot(t,fourier_x,'b');
plot(t,fourier_x_hp,'r');
title('Triangle Waves(w0<<1/RC)');





%w=1/RC
R=100;
C=0.01;
H_hp_p = (1i*k*2*pi*R*C/T)./(1+1i*k*2*pi*R*C/T);
r_p = abs(H_hp_p);
theta_p = angle(H_hp_p);

fourier_x_hp=0;
for i = 1:1:20
    fourier_x_hp = fourier_x_hp - 8*r_p(i)/(k(i)^2 * pi^2) * cos(2*pi*k(i)*t/T + theta_p(i));
end
figure(3)
plot(t,fourier_x,'b');
plot(t,fourier_x_hp,'r');
title('Triangle Waves(w0=1/RC)');



%w>1/RC
R=100;
C=1;
H_hp_p = (1i*k*2*pi*R*C/T)./(1+1i*k*2*pi*R*C/T);
r_p = abs(H_hp_p);
theta_p = angle(H_hp_p);

fourier_x_hp=0;
for i = 1:1:20
    fourier_x_hp = fourier_x_hp - 8*r_p(i)/(k(i)^2 * pi^2) * cos(2*pi*k(i)*t/T + theta_p(i));
end
figure(4)
plot(t,fourier_x,'b')
plot(t,fourier_x_hp,'r');
title('Triangle Waves(w0>1/RC)');
